<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Feb 4, 2013
 * Time: 4:05:34 PM
 * To change this template use File | Settings | File Templates.
 */

class Holder {
    private $db;
    private $id;
    private $lotName;
    private $color;
    private $price;
    private $material;
    private $specification;
    private $supplier;
    private $quantity;
    private $receivedDate;
    private $codeRange;
    private $lotNameArray;

    public function __construct() {
        $ado = new AdodbDriver();
        $this->db = $ado->pConnect(true);
        $this->db->debug = false;
        $this->setLotNameArray();
    }

    function __set($name, $value) {
        // TODO: Implement __set() method.
        $this->$name = $value;

    }

    function __get($name) {
        // TODO: Implement __get() method.
        return $this->$name;
    }

    public function setLotNameArray() {
        $sql = "SELECT id,lot_name from aea_holder_lot order by id asc";
        $dep_code = $this->db->Execute($sql);
        $this->lotNameArray = $dep_code->GetAssoc();

    }

    public function validateCardId($id) {
        $sql = "SELECT  id FROM aea_holder Where UPPER(code)=UPPER('$id') LIMIT 1";
        $recordSet = $this->db->Execute($sql);
        return !$recordSet->FetchRow();
    }

    public function GetLotListMenu() {
        //$sql = 'select name,id from aea_department';
        $sql = 'select lot_name,id from aea_holder_lot where is_delete=0';
        $sectionMenuList = $this->db->Execute($sql);
        return $sectionMenuList->GetMenu2('searchCriteria.lotName', '', ':--All select--', '', '', 'id="searchCriteria.lotName" onchange="doSearch(event)" style="width:150px;"');
    }

    public function save() {
        ADOdb_Active_Record::SetDatabaseAdapter($this->db);
        try {
            $tldLot = new TldLotAR();
            if ($this->id != '' AND $this->id != NULL) {
                $tldLot->Load("id=$this->id");
            } else {

                $tldLot->create_time = date('Y-m-d H:i:s', time());
                $tldLot->create_user_id = $_SESSION['user_id'];
                $tldLot->is_delete = 0;
            }
            $tldLot->lot_name = $this->lotName;
            $tldLot->color = $this->color;
            $tldLot->price = $this->price;
            $tldLot->material = $this->material;
            $tldLot->specification = $this->specification;
            $tldLot->supplier = $this->supplier;
            $tldLot->quantity = $this->quantity;
            $tldLot->received_date = $this->receivedDate;
            $tldLot->code_range = $this->codeRange;
            $tldLot->update_user_id = $_SESSION['user_id'];
            $tldLot->version = $this->version;
            $i = $tldLot->save();
        } catch (exception $e) {
            //var_dump($e);
            print_r($e);
            adodb_backtrace($e->gettrace());
        }
        return $i;
    }

    public function delete() {
        ADOdb_Active_Record::SetDatabaseAdapter($this->db);
        try {
            $tldLot = new TldLotAR();
            if ($this->id != '' AND $this->id != NULL) {
                $tldLot->Load("id=$this->id");
                $tldLot->is_delete = 1;
                $tldLot->update_user_id = $_SESSION['user_id'];
                $i = $tldLot->save();
            }

        } catch (exception $e) {
            //var_dump($e);
            print_r($e);
            adodb_backtrace($e->gettrace());
        }
        return $i;
    }

    public function getGrid($posStart, $count, $sort_by, $sort_dir) {
        $sql = "SELECT * FROM aea_holder_lot Where is_delete=0";
        if ($sort_dir == '')
            $sort_dir = "asc";
        if ($sort_by != '')
            $sql .= " Order By $sort_by $sort_dir";
        if ($posStart == 0) {
            $sqlCount = "Select count(*) as cnt from ($sql) as tbl";

            $resCount = $this->db->Execute($sqlCount);
            if (!$resCount)
                print $this->db->ErrorMsg();
            else
                while (!$resCount->EOF) {
                    $totalCount = $resCount->fields[0];
                    $resCount->MoveNext();
                }

        }
        $sql .= " LIMIT " . $posStart . "," . $count;
        $res = $this->db->Execute($sql);
        if ($res) {
            $xmlString = "<?xml version=\"1.0\"  encoding=\"ISO-8859-1\"?><rows total_count='" . $totalCount . "' pos='" . $posStart . "'>";
            while (!$res->EOF) {
                $xmlString .= "<row id='" . $res->fields['id'] . "'>
                    <cell>" . $res->fields['lot_name'] . "</cell>
                    <cell>" . $res->fields['color'] . "</cell>
                    <cell>" . $res->fields['code_range'] . "</cell>
                    <cell>" . $res->fields['received_date'] . "</cell>
                    <cell>" . $res->fields['price'] . "</cell>
                    <cell>" . $res->fields['material'] . "</cell>
                    <cell>" . $res->fields['specification'] . "</cell>
                    <cell>" . $res->fields['supplier'] . "</cell>
                    <cell>" . $res->fields['quantity'] . "</cell>
                    </row>";
                $posStart++;
                $res->MoveNext();
            }
            $xmlString .= "</rows>";
        } else {
            $xmlString = mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        return $xmlString;
    }

    public function getGridHolder($code, $lotId, $posStart, $count, $sort_by, $sort_dir) {
        $sql = "SELECT * FROM aea_holder Where is_delete=0";
        if ($lotId != '')
            $sql .= " and lot_id = '$lotId'";
        if ($code != '')
            $sql .= " and code like '$code%'";
        if ($sort_dir == '')
            $sort_dir = "asc";
        if ($sort_by != '')
            $sql .= " Order By $sort_by $sort_dir";
        if ($posStart == 0) {
            $sqlCount = "Select count(*) as cnt from ($sql) as tbl";

            $resCount = $this->db->Execute($sqlCount);
            if (!$resCount)
                print $this->db->ErrorMsg();
            else
                while (!$resCount->EOF) {
                    $totalCount = $resCount->fields[0];
                    $resCount->MoveNext();
                }

        }
        $sql .= " LIMIT " . $posStart . "," . $count;
        $res = $this->db->Execute($sql);
        if ($res) {
            $xmlString = "<?xml version=\"1.0\"  encoding=\"ISO-8859-1\"?><rows total_count='" . $totalCount . "' pos='" . $posStart . "'>";
            while (!$res->EOF) {
                $xmlString .= "<row id='" . $res->fields['id'] . "'>
                        <cell>" . $res->fields['id'] . "</cell>
                        <cell>" . $this->lotNameArray[$res->fields['lot_id']] . "</cell>
                        <cell>" . $res->fields['code'] . "</cell>
                        <cell>" . $res->fields['is_pass_test'] . "</cell>
                        <cell>" . $res->fields['is_dosimeter'] . "</cell>
                        <cell>" . $res->fields['is_qa_card'] . "</cell>
                        </row>";
                $posStart++;
                $res->MoveNext();
            }
            $xmlString .= "</rows>";
        } else {
            $xmlString = mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        return $xmlString;
    }


}

require_once('libraries/adodb5/adodb-active-record.inc.php');
class TldLotAR extends ADOdb_Active_Record {
    var $_table = 'aea_holder_lot';

}